//557. 反转字符串中的单词 III
//简单
//相关标签
//premium lock icon
//相关企业
//给定一个字符串 s ，你需要反转字符串中每个单词的字符顺序，同时仍保留空格和单词的初始顺序。
class Solution {
public:
    void Reverse(string& s, int start, int end)
    {
        char tmp;
        while (start < end)
        {
            tmp = s[start];
            s[start] = s[end];
            s[end] = tmp;
            start++;
            end--;
        }
    }
    string reverseWords(string s) {
        size_t start = 0;
        size_t end = 0;
        while (start < s.size())
        {
            end = s.find(' ', start);
            if (end == string::npos)
            {
                end = s.size();
                break;
            }
            Reverse(s, start, end - 1);
            start = end + 1;
        }
        Reverse(s, start, end - 1);
        return s;
    }
};